﻿
using System.Text;
using System;

namespace IDEAMS.utils
{
    public class DataConverter
    {
        public static int ToInt16(byte[] instance, int offset)
        {

            return (int)(instance[offset++] | instance[offset++] << 8);
        }
        public static int ToInt32(byte[] instance, int offset)
        {

            return (int)(instance[offset++] | instance[offset++] << 8 | instance[offset++] << 16 | instance[offset] << 24);
        }

        public static void Int16ToBytes(byte[] instance, int offset, Int16 value)
        {
            instance[offset++] = (byte)(value & 0x00FF);
            instance[offset++] = (byte)(value >> 8);
        }

        public static string ipAddressToString(byte[] ipAddress)
        {
            if (ipAddress.Length < 4) return null;

            return string.Format("{0}.{1}.{2}.{3}",
                ipAddress[0].ToString(), ipAddress[1].ToString(), ipAddress[2].ToString(), ipAddress[3].ToString());
        }

        public static byte[] stringToIPAddress(string str)
        {
            byte[] rn = new byte[4];
            try
            {
                string[] subStr = str.Split('.');
                if (subStr.Length < 4) return null;

                rn[0] = byte.Parse(subStr[0]);
                rn[1] = byte.Parse(subStr[1]);
                rn[2] = byte.Parse(subStr[2]);
                rn[3] = byte.Parse(subStr[3]);

            }
            catch (Exception)
            {
                rn = null;
            }

            return rn;
        }

        public static byte[] stringToMacAddress(string str)
        {
            byte[] rn = new byte[6];
            try
            {
                string[] subStr = str.Split(':');
                if (subStr.Length < 6) return null;

                rn[0] = byte.Parse(subStr[0], System.Globalization.NumberStyles.HexNumber);
                rn[1] = byte.Parse(subStr[1], System.Globalization.NumberStyles.HexNumber);
                rn[2] = byte.Parse(subStr[2], System.Globalization.NumberStyles.HexNumber);
                rn[3] = byte.Parse(subStr[3], System.Globalization.NumberStyles.HexNumber);
                rn[4] = byte.Parse(subStr[4], System.Globalization.NumberStyles.HexNumber);
                rn[5] = byte.Parse(subStr[5], System.Globalization.NumberStyles.HexNumber);
            }
            catch (Exception)
            {
                rn = null;
            }

            return rn;
        }

        public static string macAddressToString(byte[] macAddress)
        {
            if (macAddress.Length < 6) return null;

            return string.Format("{0}:{1}:{2}:{3}:{4}:{5}",
                macAddress[0].ToString("x2"), macAddress[1].ToString("x2"), macAddress[2].ToString("x2"),
                macAddress[3].ToString("x2"), macAddress[4].ToString("x2"), macAddress[5].ToString("x2"));
        }

        public static int ToUnixTimeStamp(DateTime dateTime)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (Int32)((dateTime - dtStart).TotalSeconds);
        }

        // 字符串转固定长度数组
        public static byte[] getBytes(String s, int length)
        {
            byte[] S_bytes = new byte[length];
            if (Encoding.Default.GetBytes(s).Length <= length)
            {
                Array.Copy(Encoding.Default.GetBytes(s), 0, S_bytes, 0, Encoding.Default.GetBytes(s).Length);
            }
            else
            {
                Array.Copy(Encoding.Default.GetBytes(s), 0, S_bytes, 0, length);
            }
            return S_bytes;
        }

        public static int ToUInt16(byte[] instance, int offset)
        {
            return (int)(instance[offset++] | instance[offset] << 8);
        }
    }
}
